﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SheBeiGLXT.Models;

namespace SheBeiGLXT.Controllers
{
    public class DefaultController : Controller
    {
        Model1 model1 = new Model1();
        // GET: Default
        public ActionResult Index(int id = 0, string Content = "")
        {
            //获取所有用户列表
            var users = model1.Device.ToList();
            //在用户列表开始位置添加全部选项
            users.Insert(0, new Device() { DeviceName = "全部", DeviceID = 0 });
            //生成下拉框数据，属性名与实体属性保持一致
            ViewBag.id = new SelectList(users, "DeviceID", "DeviceName");
            //根据条件查询数据
            var list = model1.Defect.Where(p => p.Content.Contains(Content) && (id == 0 || p.DeviceID == id)).ToList();
            return View(list);
        }

        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return RedirectToAction("Index");
            }
            var task = model1.Defect.FirstOrDefault(p => p.DefectID == id);
            if (task == null)
            {
                return RedirectToAction("Index");
            }
            //生成下拉框数据，属性名与实体属性保持一致
            ViewBag.id = new SelectList(model1.Device, "DeviceID", "DeviceName", task.DeviceID);
            return View(task);
        }
        [HttpPost]
        public ActionResult Edit(Defect defect)
        {
            if (ModelState.IsValid)
            {
                var temp = model1.Defect.FirstOrDefault(p => p.DefectID == defect.DefectID);
                if (temp == null)
                {
                    return RedirectToAction("Index");
                }
                temp.Content = defect.Content;
                temp.DealTime = defect.DealTime;
                temp.DefectType = defect.DefectType;
                temp.DefectState = defect.DefectState;
                temp.DeviceID = defect.DeviceID;
                var res = model1.SaveChanges();
                if (res > 0)
                {
                    return RedirectToAction("Index");
                }
            }
            return View(defect);
        }

        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return Json(false);
            }
            var defect = model1.Defect.FirstOrDefault(p => p.DefectID == id);
            if (defect == null)
            {
                return Json(false);
            }
            model1.Defect.Remove(defect);
            var res = model1.SaveChanges();
            return Json(res > 0);
        }

        public ActionResult Create()
        {
            //生成下拉框数据，属性名与实体属性保持一致
            ViewBag.id = new SelectList(model1.Device, "DeviceID", "DeviceName");
            return View();
        }
        [HttpPost]
        public ActionResult Create(Defect defect)
        {
            if (ModelState.IsValid)
            {
                //与用户交互的属性，直接使用传递的数据
                //特殊处理：未与用户交互，且默认值无法满足数据库要求的属性必须重新赋值（数据库默认值）
                defect.AddTime = DateTime.Now;
                defect.DefectState = 1;
                defect.DealTime = null;
                model1.Defect.Add(defect);
                var res = model1.SaveChanges();
                if (res > 0)
                {
                    return RedirectToAction("Index");
                }
            }
            ViewBag.id = new SelectList(model1.Device, "DeviceID", "DeviceName");
            return View(defect);
        }
    }
}